/**
 * 首页
 * @constructor
 */
function Home() {
}

Home.init = function () {
    getIncome();
    getVisitors();
    getMember();
    getRoom();
    getEchartMap('week');
}

//获得本日营业收入
function getIncome() {
    //发送异步请求到后台保存用户数据
    $.ajax({
        url: "/home/getIncome",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $("#h_FarmNum").text(data)
        }
    });
}

//获得本日访问量
function getVisitors() {
    //发送异步请求到后台保存用户数据
    $.ajax({
        url: "/home/getVisitors",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $("#h_PigNum").text(data)
        }
    });
}

//获得新增会员数量
function getMember() {
    //发送异步请求到后台保存用户数据
    $.ajax({
        url: "/home/getMember",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $("#h_ResourceRate").text(data)
        }
    });
}

//获得可用房间数
function getRoom() {
    //发送异步请求到后台保存用户数据
    $.ajax({
        url: "/home/getRoom",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $("#h_EquipmentRate").text(data)
        }
    });
}

function getEchartMap(type) {
    $("#todayButton").removeAttr("class");
    $("#weekButton").removeAttr("class");
    $("#monthButton").removeAttr("class");
    if (type == "today") {
        $("#todayButton").addClass("btn btn-xs btn-white active");
        $("#weekButton").addClass("btn btn-xs btn-white");
        $("#monthButton").addClass("btn btn-xs btn-white");
    } else if (type == "week") {
        $("#todayButton").addClass("btn btn-xs btn-white");
        $("#weekButton").addClass("btn btn-xs btn-white active");
        $("#monthButton").addClass("btn btn-xs btn-white");
    } else if (type == "month") {
        $("#todayButton").addClass("btn btn-xs btn-white");
        $("#weekButton").addClass("btn btn-xs btn-white");
        $("#monthButton").addClass("btn btn-xs btn-white active");
    }


    var myChart = echarts.init(document.getElementById('container'));
    $.ajax({
        url: "/home/getIncome2/" + type,
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            // alert("Error");
            Maps(data, myChart)
        },
        error: function () {
            // alert("Error");
        }
    });
}

function Maps(mapData, myChart) {

    console.log("mapData",mapData)
    option = {
        xAxis: {
            type: 'category',
            data:mapData.date,
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: mapData.money,
            type: 'line'
        }]
    };


    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option, true); //如果不设置TRUE就不能动态加载图
    window.onresize = myChart.resize;

}